<?php
	$liked = $fb_data['fan']['page']['liked'];
	$admin = $fb_data['fan']['page']['admin'];
  $pageId= $fb_data['fan']['page']['id'];
	if($admin){
?>
  <div id="content">
    <div id="wrapper">
      <h1>LIKE GATE DASHBOARD</h1>
      <div id="steps">
      <?php
        $liked_sql = 'SELECT * FROM pages WHERE page_id = "'.$pageId.'"';
        $liked_q = $db->select_query($liked_sql);
        $liked_rs= $db->fetch($liked_q);
      ?>
        <form id="formElem" name="formElem" action="" method="post">
          <fieldset class="step">
            <legend>LIKE GATE</legend>
            <p>
              <label for="fan">FAN</label>
              <textarea rows="10" id="fan" name="fan" class="wysiwyg">
                <?php echo $liked_rs['liked_page']; ?>
              </textarea>
            </p>
            <p>
              <label for="guest">GUEST</label>
              <textarea rows="10" id="guest" name="guest" class="wysiwyg">
                <?php echo $liked_rs['guest_page']; ?>
              </textarea>
            </p>
            <p class="submit">
              <input type="hidden" name="pageId" id="pageId" value="<?php echo $pageId;?>" />  
              <button id="registerButton" type="submit">Register</button>
            </p>
          </fieldset>
          <fieldset class="step">
              <legend>FAN PAGE</legend>
              <div id="fanPage"><?php echo html_entity_decode($liked_rs['liked_page']); ?></div>
          </fieldset>
          <fieldset class="step">
            <legend>GUEST PAGE</legend>
            <div id="guestPage"><?php echo html_entity_decode($liked_rs['guest_page']); ?></div>            
          </fieldset>
        </form>
    </div>
    <div id="navigation" style="display:none;">
        <ul>
            <li class="selected">
                <a href="#">Like Gate</a>
            </li>
            <li>
                <a href="#">Fan Page</a>
            </li>
            <li>
                <a href="#">Guest Page</a>
            </li>
        </ul>
        <span class="reference">
            <a href="http://www.codeponpon.com/">Power by codePONPON</a>
        </span>
      </div>
    </div>
  </div>
<script type="text/javascript">
$(function(){
  $('#formElem').submit(function(){
    var fan_page    = $('#fan').val();
    var guest_page  = $('#guest').val();
    var page_id     = $('#pageId').val();
    $.post('../query.php',{
      likeGate:true, 
      pageId:page_id,
      fanPage:fan_page, 
      guestPage:guest_page
    },function(result){
        if(result.status){
          $('.step #fanPage').html(fan_page);
          $('.step #guestPage').html(guest_page);
          alert('Successful You can click the navigation to see each page.');
        }else{
          alert('Error!!');
        }
    },'json');
    return false;
  });
});
</script>
<?php   
  }else{
    if($liked){
      $sql = 'SELECT liked_page FROM '.PAGES.' WHERE page_id = "'.$pageId.'"';
      $query = $db->select_query($sql);
      $rs = $db->fetch($query);
      $page_content = $rs['liked_page'];
    }else{
      $sql = 'SELECT guest_page FROM '.PAGES.' WHERE page_id = "'.$pageId.'"';
      $query = $db->select_query($sql);
      $rs = $db->fetch($query);
      $page_content = $rs['guest_page'];
    }
    echo html_entity_decode($page_content);
?>
<?php $app_id = $fb->getAppId(); ?>
<span class="add_tab_to_page">
    <a href="#" onclick=window.open("http://www.facebook.com/dialog/pagetab?app_id=<?php echo $app_id; ?>&next=http://localhost/fb/link_gate/","PageTab","width=960,height=400");>Add LikeGate to Your Fan Page</a>
    <a href="http://www.codeponpon.com/">Power by codePONPON</a>
</span>
<?php   
  }
?>